package yxy.game.pm2.command.shop;

import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.ShopMessage;
import yxy.game.pm2.module.CurrencyHandle;
import yxy.game.pm2.module.shop.HeroShopModule;
import yxy.game.pm2.module.shop.handle.HeroShopHandle;


/** 商店 武将商店刷新 */
@MessageType(proto = Command.HeroShopUpdate, classtype = ShopMessage.HeroShopUpdateRequest.class)
public class CMD_HeroShopUpdate extends Command<ShopMessage.HeroShopUpdateRequest> {

	@Override
	public void execute(MessageSource messageSource, ShopMessage.HeroShopUpdateRequest request) {
		try {
			String playerCode = messageSource.getTag();

			ShopMessage.HeroShopUpdateResponse.Builder response = ShopMessage.HeroShopUpdateResponse.newBuilder();

			Result result = new HeroShopHandle().refresh(playerCode);
			if (result == Result.Success) {
				//根据开服时间和showtime过滤商品是否展示
				response.addAllGoods(new HeroShopHandle().checkShopItemIsShow(playerCode));
				response.setResetCost(new CurrencyHandle().packPoint(HeroShopModule.ShopRefreshCost()));
			}

			send(messageSource, response, result);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void send(MessageSource messageSource, ShopMessage.HeroShopUpdateResponse.Builder response, Result result) {
		response.setResult(result.packResult());
		MessageEmitterHandler.getInstance().send(messageSource, response.build());
	}
}